package com.erp.common.controller;

import com.erp.common.ResponseMessage;
import com.erp.common.service.BaseService;
import com.erp.common.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public abstract class BaseControllerPowerful<T> {

    protected abstract BaseService getService();


    /**
     * 新增实体
     * @param entity
     * @return
     */
    @ApiOperation("新增实体信息")
    @PostMapping("/add")
    public ResponseMessage add(T entity){
        if (getService().add(entity)) {
            return ResponseMessage.ok();
        } else{
            return ResponseMessage.fail("添加失败，请重试");
        }

    }

    /**
     * 删除实体（支持多选）
     * @param ids
     * @return
     */
    @ApiOperation("删除实体信息（支持多选）")
    @PostMapping("/list/delete")
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids){
        if (getService().deleteIds(ids)){
            return ResponseMessage.ok();
        } else {
            return ResponseMessage.fail("删除失败，请重试");
        }
    }

    /**
     *  更新实体信息
     * @param entity
     * @return
     */
    @ApiOperation("更新实体信息")
    @PostMapping("/update")
    public ResponseMessage update(T entity){
        if (getService().update(entity)) {
            return ResponseMessage.ok();
        } else{
            return ResponseMessage.fail("更新失败，请重试");
        }
    }

    /**
     *  实体列表（分页）
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("分页展示实体信息")
    @GetMapping("/list/{page}/{size}")
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size){
        List list = getService().listPage(page, size);
        if (list.size()>0){
            return new ResponseMessage(list);
        } else {
            return ResponseMessage.fail("无相关数据");
        }
    }

    /**
     *  模糊查询实体列表（分页）
     * @param page
     * @param size
     * @param column
     * @return
     */
    @ApiOperation("分页展示实体信息（带模糊查询）")
    @GetMapping("/list/name/{page}/{size}")
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size")int size,String column){
        List list = getService().listPageLike(page, size, StringUtil.addLike(column));
        if (list.size()>0){
            return new ResponseMessage(list);
        } else {
            return ResponseMessage.fail("无相关数据");
        }
    }
}
